// import Cookies from 'js-cookie'

const TokenKey = 'DEMO-USER-TOKEN'
const TempTokenKey = 'DEMO-USER-TEMP-TOKEN'
const TicketKey = 'DEMO-USER-TICKET'

let domain = null

// 根据环境来设置接口请求
if (process.env.NODE_ENV === 'development') {
  domain = null // dev分支
} else if (process.env.NODE_ENV === 'debug') {
  domain = null // 调试分支
} else if (process.env.NODE_ENV === 'production') {
  domain = 'demo.itzxf.com' // 生产分支
}
/**
 * ======================================================================
 */
// 创建cookie this.setCookie('test', '123456', 7, null,'abc.com')
export function setCookie (name, value, expires, path, domain, secure) {
  var cookieText = encodeURIComponent(name) + '=' + encodeURIComponent(value)
  if (typeof expires === 'number') {
    expires = new Date(new Date() * 1 + expires * 864e+5)
  }
  // 我们使用“expires”，因为IE不支持“max-age”
  expires = expires ? expires.toUTCString() : ''
  if (expires) {
    cookieText += '; expires=' + expires
  }
  if (path) {
    cookieText += '; path=' + path
  }
  if (domain) {
    cookieText += '; domain=' + domain
  }
  if (secure) {
    cookieText += '; secure'
  }
  document.cookie = cookieText
}

// 获取cookie
export function getCookie (name) {
  var cookieName = encodeURIComponent(name) + '='
  var cookieStart = document.cookie.indexOf(cookieName)
  var cookieValue = null
  if (cookieStart > -1) {
    var cookieEnd = document.cookie.indexOf(';', cookieStart)
    if (cookieEnd === -1) {
      cookieEnd = document.cookie.length
    }
    cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd))
  }
  return cookieValue
}
// 删除cookie
export function delCookie (name) {
  document.cookie = name + '= ; expires=' + new Date(0) - 3600
}

/**
 * ======================================================================
 */
// 用户token
export function getToken () {
  // return Cookies.get(TokenKey)
  return getCookie(TokenKey)
}

export function getTempToken () {
  // return Cookies.get(TempTokenKey)
  return getCookie(TempTokenKey)
}

export function setToken (token) {
  // return Cookies.set(TokenKey, token, { expires: 7 })
  return setCookie(TokenKey, token, 7, null, domain)
}

export function setTempToken (token) {
  // return Cookies.set(TempTokenKey, token, { expires: 7 })
  return setCookie(TempTokenKey, token, 7, null, domain)
}

// 设置用户自动登录使用的 ticket
export function setTicketToken (ticket) {
  // return Cookies.set(TicketKey, ticket, { expires: 7 })
  return setCookie(TicketKey, ticket, 7, null, domain)
}

// 设置用户自动登录使用的 ticket
export function getTicketToken () {
  // return Cookies.get(TicketKey)
  return getCookie(TicketKey)
}
export function removeToken () {
  // return Cookies.remove(TokenKey)
  setCookie(TokenKey, '', -1, null, domain)
  return delCookie(TokenKey)
}
export function removeTicket () {
  // return Cookies.remove(TicketKey)
  setCookie(TicketKey, '', -1, null, domain)
  return delCookie(TicketKey)
}
export function removeTempToken () {
  // return Cookies.remove(TempTokenKey)
  setCookie(TempTokenKey, '', -1, null, domain)
  return delCookie(TempTokenKey)
}
